#include "M3uHandler.h"
Playlist::Playlist(){

}
Playlist::Playlist(string fileName){
	listFile.assign(fileName);
}
void Playlist::openFile(){
	string content;
	ifstream fichier(listFile.c_str());
	if(!fichier)
		cout << "no playlist file found" << endl;
	else{
		getline(fichier,content);
		if (content=="#EXTM3U"){
			while(getline(fichier,content)){
				if(!content.find("#EXTINF:")){
					string buffer;
					string itemName;
					string itemUrl;
					string sItemCode;
					string mediaType;
					string url;
					int itemCode;
					istringstream iss(content);
					getline(iss,buffer,':');
					getline(iss,buffer,',');
					getline(iss,sItemCode,' ');
					getline(iss,buffer,'-');
					getline(iss,buffer,' ');
					getline(iss,itemName,',');
					istringstream code(sItemCode);
					code >> itemCode;
					getline(fichier,content);
					istringstream iss2(content);
					getline(iss2,url,' ');
					getline(iss2,buffer,'-');
					getline(iss2,buffer,' ');
					getline(iss2,mediaType,',');
					Channel channel1(itemName,itemCode,url,mediaType);
					channelList.insert(pair<int, Channel>(itemCode,channel1));
				}
			}
		}
		else
			cout << "invalid format file";		}
	fichier.close();
}
void Playlist::addChannel(Channel c){
	channelList.insert(pair<int, Channel>(c.getCode(),c));
}
void Playlist::addChannel(string name, int code, string url,string mediaType){
	Channel *c1 = new Channel(name,code,url,mediaType);
	channelList.insert(pair<int, Channel>(c1->getCode(),*c1));
}
Channel Playlist::getChannel(int code){
	map<int,Channel>::const_iterator mapIt;
	mapIt = channelList.find(code);
	if(mapIt != channelList.end()){
		return mapIt->second;
	}
	/*else{
		return NULL;
	}*/
}
Channel Playlist::chooseChannel(){
	int channelNb;
	map<int,Channel>::const_iterator mapIt;
	cout<<"*** List of media ***"<<endl;
	for(mapIt=channelList.begin();mapIt !=channelList.end();mapIt++){
		Channel c = mapIt->second;
		cout << c.getCode() <<"  ---  " << c.getName() << endl;
	}
	cout <<"************************"<<endl;
	cout <<"Please enter a channel number : "<<endl<<"> ";

	string buffer;
	getline(cin, buffer);
	istringstream iss(buffer);
	iss >> channelNb;

	map<int,Channel>::const_iterator mapIt2;
	mapIt2 = channelList.find(channelNb);
	Channel chan = mapIt2->second;
	if(mapIt2 != channelList.end())
		return chan;
	/*else
		return NULL;*/
}
void Playlist::configureList(){
	map<int,Channel>::const_iterator mapIt;
	ofstream fichier;
	fichier.open(listFile.c_str(),ios::app);
	cout<<"Updating .m3u file"<<endl;
	for(mapIt=channelList.begin();mapIt !=channelList.end();mapIt++){
		Channel c = mapIt->second;
		fichier << "#EXTINF:0," << c.getCode() << " - " << c.getName() << endl;
		fichier << c.getUrl() << " - " << c.getMediaType() << endl << endl;
	}
	fichier.close();
}
void Playlist::restoreList(){
	string content;
	map<int,Channel> temp;
	map<int,Channel>::const_iterator mapIt;
	ifstream fichier(listFile.c_str());
	if(!fichier)
		cout << "no playlist file found" << endl;
	else{
		getline(fichier,content);
		if (content=="#EXTM3U"){
			while(getline(fichier,content)){
				if(!content.find("#EXTINF:")){
					string buffer;
					string itemName;
					string itemUrl;
					string sItemCode;
					string url;
					string mediaType;
					int itemCode;
					istringstream iss(content);
					getline(iss,buffer,':');
					getline(iss,buffer,',');
					getline(iss,sItemCode,' ');
					getline(iss,buffer,'-');
					getline(iss,buffer,' ');
					getline(iss,itemName,',');
					istringstream code(sItemCode);
					code >> itemCode;
					getline(fichier,content);
					istringstream iss2(content);
					getline(iss2,url,' ');
					getline(iss2,buffer,'-');
					getline(iss2,buffer,' ');
					getline(iss2,mediaType,',');
					cout<< mediaType<<endl;
					Channel channel1(itemName,itemCode,url,mediaType);
					temp.insert(pair<int, Channel>(itemCode,channel1));
				}
			}
		}
		else
			cout << "invalid format file";		}
	fichier.close();
	//boucle pour effacer les chaines de la playlist dans la liste temporaire
	for(mapIt=channelList.begin();mapIt !=channelList.end();mapIt++){
		Channel c = mapIt->second;
		temp.erase(c.getCode());
	}
	//remplacement du fichier par le contenu de la liste temporaire
	ofstream fichierOut(listFile.c_str(), ios::out | ios::trunc);
	if(fichierOut){
		fichierOut << "#EXTM3U" << endl << endl;
		for(mapIt=temp.begin();mapIt !=temp.end();mapIt++){
			Channel c = mapIt->second;
			fichierOut << "#EXTINF:0," << c.getCode() << " - " << c.getName() << endl;
			fichierOut << c.getUrl()  << " - " << c.getMediaType() << endl << endl;
		}
	}
}


unsigned int Playlist::getNumberOfChannel()
{
	return channelList.size();
}
